﻿using AutoMapper;
using B.S.Filtration.Domain.专检;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Read.Api.Application.Command.ProductionTaskCommand;
using MediatR;
using NPOI.SS.Formula.Functions;
using SqlSugar;

namespace B.S.Filtration.Read.Api.Application.CommandHander.ProductionTaskHandler
{
    public class GetProductionTaskHandler : IRequestHandler<GetProductionTaskCommand, APIResult<APIPaging<ProductionTask>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public GetProductionTaskHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<APIPaging<ProductionTask>>> Handle(GetProductionTaskCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<ProductionTask>> reat = new APIResult<APIPaging<ProductionTask>>();
            var listtt = await db.Queryable<ProductionTask>().Where(x => !x.IsDel).ToListAsync();

            int totalCount = listtt.Count();
            int PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            listtt = listtt.OrderByDescending(a => a.id).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            APIPaging<ProductionTask> paging = new APIPaging<ProductionTask>()
            {
                TotalCount = totalCount,
                TotalPage = PageCount,
                list = listtt.ToList(),

            };

            reat.code = APIResultCode.success;
            reat.msg = "专检显示成功";
            reat.data = paging;
            return reat;
        }
    }
}
